package com.ajk.message.util;

import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ajk.message.config.JpushProperties;
import com.ajk.message.model.JPushNotification;
import com.ajk.message.model.JPushPlatform;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class PushUtil {
	
	private static Logger logger = LoggerFactory.getLogger(PushUtil.class);
	
	static{
		ClientConfig config = ClientConfig.getInstance();
		config.setMaxRetryTimes(3);
	}
	
	private static JpushProperties jpushProperties;
	
	public void setJpushProperties(JpushProperties jpushProperties) {
		PushUtil.jpushProperties = jpushProperties;
	}
	
	public static JsonObject notification(JPushNotification noticfication){
		JsonObject obj = new JsonObject();
		String uuid = java.util.UUID.randomUUID().toString();;
		
		Builder builder = PushPayload.newBuilder();
		builder.setAudience(Audience.alias(noticfication.getAlias()));
		switch(noticfication.getPlatform()){
			case JPushPlatform.ios:
				builder.setPlatform(Platform.ios());
				break;
			case JPushPlatform.android:
				builder.setPlatform(Platform.android());
				break;
			case JPushPlatform.android_ios:
				builder.setPlatform(Platform.android_ios());
				break;
		}
		Notification.Builder notificationBulder = Notification.newBuilder();
		if((noticfication.getPlatform() & JPushPlatform.ios) == JPushPlatform.ios){
			IosNotification.Builder iosNotificationBuilder = IosNotification.newBuilder().setAlert(noticfication.getAlert()).setSound("default").incrBadge(1);
			JsonObject extras = noticfication.getExtras();
			if(extras!=null){
				for(Entry<String, JsonElement> entry : extras.entrySet()){
					iosNotificationBuilder.addExtra(entry.getKey(), entry.getValue().getAsString());
				}
			}
			notificationBulder.addPlatformNotification(iosNotificationBuilder.build());
		}
		if((noticfication.getPlatform() & JPushPlatform.android) == JPushPlatform.android){
			AndroidNotification.Builder androidNotificationBuilder = AndroidNotification.newBuilder();
			androidNotificationBuilder.setTitle(noticfication.getTitle()).setAlert(noticfication.getAlert());
			JsonObject extras = noticfication.getExtras();
			if(extras!=null){
				for(Entry<String, JsonElement> entry : extras.entrySet()){
					androidNotificationBuilder.addExtra(entry.getKey(), entry.getValue().getAsString());
				}
			}
			notificationBulder.addPlatformNotification(androidNotificationBuilder.build());
		}
		builder.setNotification(notificationBulder.build());

		Options.Builder optionsBuilder = Options.newBuilder();
		if(noticfication.getSendno()!=null){
			optionsBuilder.setSendno(noticfication.getSendno());
		}
		optionsBuilder.setTimeToLive(86400);
		optionsBuilder.setApnsProduction("1".equals(jpushProperties.getFlag())?true:false);
		builder.setOptions(optionsBuilder.build());
		
		PushPayload payLoad = builder.build();
		
		JPushClient jpushClient = new JPushClient(jpushProperties.getSecret(), jpushProperties.getAppkey());
		PushResult result = null;
		try {
			result = jpushClient.sendPush(payLoad);
		} catch (APIConnectionException | APIRequestException e) {
			logger.error("发送推送失败，调用参数{}",new Gson().toJson(noticfication));
		}
		if(result!=null){
			obj.addProperty("code",result.getResponseCode());
			obj.addProperty("message", result.getOriginalContent());
			obj.addProperty("msgId", uuid);
		}
		return obj;
	}
	
	
	
}
